package com.stn.zjx.givingtreasure.net.impl;

import android.annotation.SuppressLint;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.stn.zjx.givingtreasure.net.EthApi;
import com.stn.zjx.givingtreasure.net.UserApi;
import com.stn.zjx.givingtreasure.utils.ToolsUtils;
import com.vondear.rxtools.view.RxToast;


import org.json.JSONObject;

public class EthNetImpl{

    /**
     * @apiNote 创建ETH钱包

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void create_eth_wallet(String walletPassword
            , String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("walletPassword",walletPassword);

            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.create_eth_wallet)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .params("data",data)
                .execute(callback);
    }
    /**
     * @apiNote 获取ETH,以及代币余额

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void get_eth_balance(String ethAddress,
                                       String ethTokenAddress,
                                       Integer ethTokenDecimal,
                                       String ethTokenSymbol
            , String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("ethAddress",ethAddress);
            jsonObject.put("ethTokenAddress",ethTokenAddress);
            jsonObject.put("ethTokenDecimal",ethTokenDecimal);
            jsonObject.put("ethTokenSymbol",ethTokenSymbol);

            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.get_eth_balance)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .cacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                .cacheKey(tag)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params("data",data)
                .execute(callback);
    }
    /**
     * @apiNote 获取ETH手续费

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void get_eth_charge(
            String tag, StringCallback callback){

        OkGo.<String>post(EthApi.get_eth_charge)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .cacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                .cacheKey(tag)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(callback);
    }

    /**
     * @apiNote 导出私钥

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void export_eth_privatekey(String ethAddress,
            String walletPassword
            , String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("ethAddress",ethAddress);
            jsonObject.put("walletPassword",walletPassword);
            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.export_eth_privatekey)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .params("data",data)
                .execute(callback);
    }

    /**
     * @apiNote 导出keystore

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void export_eth_keystore(String ethAddress,
                                             String walletPassword
            , String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("ethAddress",ethAddress);
            jsonObject.put("walletPassword",walletPassword);
            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.export_eth_keystore)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .params("data",data)
                .execute(callback);
    }

    /**
     * @apiNote 导入助记词，ETH钱包

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void import_eth_mnemonic(String mnemonic,String walletPassword
            , String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("mnemonic",mnemonic);
            jsonObject.put("walletPassword",walletPassword);

            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.import_eth_mnemonic)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .params("data",data)
                .execute(callback);
    }

    /**
     * @apiNote 导入keysstore，ETH钱包

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void import_eth_keystore(String keystore,String walletPassword
            , String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("keystore",keystore);
            jsonObject.put("walletPassword",walletPassword);

            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.import_eth_keystore)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .params("data",data)
                .execute(callback);
    }

    /**
     * @apiNote 导入私钥，ETH钱包

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void import_eth_privatekey(String privatekey,String walletPassword
            , String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("privatekey",privatekey);
            jsonObject.put("walletPassword",walletPassword);

            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.import_eth_privatekey)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .params("data",data)
                .execute(callback);
    }
    /**
     * @apiNote 查询地址代币列表

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void eth_token_list(String ethAddress
            , String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("ethAddress",ethAddress);
            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.eth_token_list)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .cacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                .cacheKey(tag)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params("data",data)
                .execute(callback);
    }

    /**
     * @apiNote 查询地址代币列表

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void eth_token_show(String name,String ethAddress
            , String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("name",name);
            jsonObject.put("ethAddress",ethAddress);
            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.eth_token_show)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .cacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                .cacheKey(tag)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params("data",data)
                .execute(callback);
    }

    /**
     * @apiNote 增加代币显示

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void eth_token_add_show(String ethAddress,Integer ethTokenId
            , String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("ethAddress",ethAddress);
            jsonObject.put("ethTokenId",ethTokenId);
            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.eth_token_add_show)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .params("data",data)
                .execute(callback);
    }

    /**
     * @apiNote 删除代币显示

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void eth_token_delete_show(String ethAddress,Integer ethTokenId
            , String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("ethAddress",ethAddress);
            jsonObject.put("ethTokenId",ethTokenId);
            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.eth_token_delete_show)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .params("data",data)
                .execute(callback);
    }


    /**
     * @apiNote ETH，代币转账

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void request_transfer(String symbol,String contractAddress,
                                       String fromAddress,
                                        String toAddress,
                                        String walletPassword,
                                        String value,
                                        String decimal,
                                        String gasPrice,
                                        String gasLimit,
                                        String extra,
                                        String tag,
                                        StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {


            jsonObject.put("symbol",symbol);
            jsonObject.put("contractAddress",contractAddress);
            jsonObject.put("fromAddress",fromAddress);
            jsonObject.put("toAddress",toAddress);
            jsonObject.put("walletPassword",walletPassword);
            jsonObject.put("value",value);
            jsonObject.put("decimal",decimal);
            jsonObject.put("gasPrice",gasPrice);
            jsonObject.put("gasLimit",gasLimit);
            jsonObject.put("extra",extra);
            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.request_transfer)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .params("data",data)
                .execute(callback);
    }


    /**
     * @apiNote 查询交易记录

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void query_transfer_by_address(String contractAddress,String ethAddress,
            Integer pageNow,
            Integer pageSize,
             String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("contractAddress",contractAddress);
            jsonObject.put("ethAddress",ethAddress);
            jsonObject.put("pageNow",pageNow);
            jsonObject.put("pageSize",pageSize);
            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.query_transfer_by_address)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .cacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                .cacheKey(tag)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params("data",data)
                .execute(callback);
    }
    /**
     * @apiNote 查询eth交易详情通过交易订单

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void query_transfer_txhash(String txHash,
                                                 String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("txHash",txHash);
            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(EthApi.query_transfer_txhash)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .cacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                .cacheKey(tag)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params("data",data)
                .execute(callback);
    }


    /**
     * @apiNote 分页查询行情

     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void query_market(Integer pageNow,
                                             String tag, StringCallback callback){


        OkGo.<String>get(EthApi.query_market+pageNow)
                .tag(tag)
                .cacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                .cacheKey(tag)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(callback);
    }
}
